import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:gsform/gs_form/core/form_style.dart';
import 'package:gsform/gs_form/model/data_model/spinner_data_model.dart';
import 'package:gsform/gs_form/widget/field.dart';
import 'package:gsform/gs_form/widget/form.dart';
import 'package:pass_log/app/core/utils/adapt.dart';
import 'package:pass_log/app/data/providers/type_provider.dart';
import 'package:pass_log/app/data/providers/pass_provider.dart';
import 'package:pass_log/app/data/services/theme_service.dart';

import 'pass_controller.dart';

class PassPage extends GetView<PassController> {
  const PassPage({super.key});

  static final formKey = GlobalKey<FormState>();
  static final tlp = Get.find<TypeListProvider>();
  static final plp = Get.find<PassListProvider>();

  @override
  Widget build(BuildContext context) {
    dynamic data = Get.arguments;
    String mode = Get.parameters["mode"] ?? "add";
    String typeId = data == null ? "defaultType" : data["type_id"];
    return Scaffold(
      appBar: AppBar(
        title: Text(mode == "add" ? "添加密码" : "编辑密码"),
        centerTitle: true,
      ),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: controller.form = GSForm.singleSection(
                context,
                style: GSFormStyle(
                  sectionRadius: 0,
                  sectionCardPadding: Adapt.pt(10),
                  sectionCardElevation: 0,
                  fieldBorderColor: ThemeService.to.curColorScheme.primary,
                  backgroundFieldColor: ThemeService.to.curColorScheme.onPrimary,
                  backgroundSectionColor: ThemeService.to.curColorScheme.surface,
                  requiredText: "*",
                  titleStyle: TextStyle(
                    color: Colors.black87,
                    fontSize: Adapt.pt(17),
                  ),
                  fieldHintStyle: TextStyle(
                    color: Colors.black38,
                    fontSize: Adapt.pt(15),
                  ),
                  fieldTextStyle: TextStyle(
                    color: Colors.black87,
                    fontSize: Adapt.pt(15),
                  ),
                  errorTextStyle: TextStyle(
                    color: Colors.red.shade400,
                    fontSize: Adapt.pt(12),
                  ),
                ),
                fields: [
                  GSField.text(
                    required: true,
                    tag: "title",
                    title: "密码名称",
                    hint: "请输入密码名称",
                    errorMessage: "密码名称不能为空",
                    prefixWidget: const Icon(Icons.shield),
                    value: mode == "add" ? null : data["title"],
                  ),
                  GSField.text(
                    required: true,
                    tag: "username",
                    title: "登录账号",
                    hint: "请输入登录账号",
                    errorMessage: "登录账号不能为空",
                    prefixWidget: const Icon(Icons.person),
                    value: mode == "add" ? null : data["username"],
                  ),
                  GSField.password(
                    required: true,
                    tag: "password",
                    title: "登录密码",
                    hint: "请输入登录密码",
                    errorMessage: "登录密码不能为空",
                    prefixWidget: const Icon(Icons.lock),
                    value: mode == "add" ? null : data["password"],
                  ),
                  GSField.spinner(
                    required: false,
                    tag: "type",
                    title: "密码分类",
                    prefixWidget: const Icon(Icons.bookmark),
                    items: tlp.list
                        .map((item) => SpinnerDataModel(
                              name: item["name"],
                              id: item["sort"],
                              data: item["id"],
                              isSelected: typeId == item["id"],
                            ))
                        .toList(),
                    onChange: (model) => typeId = model!.data,
                  ),
                  GSField.textPlain(
                    required: false,
                    tag: "remark",
                    title: "相关备注",
                    hint: "账号的绑定信息，如手机号、密保问题等",
                    maxLine: 6,
                    minLine: 6,
                    maxLength: 500,
                    showCounter: true,
                    value: mode == "add" ? null : data["remark"],
                  ),
                ],
              ),
            ),
          ),
          Padding(
            padding: EdgeInsets.all(Adapt.pt(10)),
            child: Row(
              children: [
                Expanded(
                  flex: 1,
                  child: ElevatedButton(
                    style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(ThemeService.to.curColorScheme.primary),
                      foregroundColor: WidgetStateProperty.all(ThemeService.to.curColorScheme.onPrimary),
                    ),
                    child: Text("确 认", style: TextStyle(fontSize: Adapt.pt(16))),
                    onPressed: () {
                      bool isValid = controller.form.isValid();
                      if (!isValid) return;
                      Map<String, dynamic> formData = controller.form.onSubmit();
                      formData["type_id"] = typeId;
                      formData.remove("type");
                      if (mode == "add") {
                        plp.createPass(formData);
                      } else {
                        formData["id"] = data["id"];
                        plp.editPass(formData);
                      }
                    },
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
